appCtrl.controller('ForgotCtrl', function($scope, $rootScope, $interval, $state, $ionicNavBarDelegate, UtilsService, UserService) {
  var _this = $scope;

  $scope.setState = function(state) {
    _this.state = state;

    switch (state) {
      case 0:
        $ionicNavBarDelegate.title("找回密码");
        break;
      case 1:
        $ionicNavBarDelegate.title("短信验证");
        break;
      case 2:
        $ionicNavBarDelegate.title("设置密码");
        break;
      default:

    }
  }

  $scope.backClicked = function() {
    if (_this.state > 0) {
      _this.state--;
    } else {
      $state.transitionTo('login');
    }
  }

  $scope.setState(0);

  $scope.getCode = function() {
    UserService.getCode({
      phone: this.phone,
      type: 1
    }).$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        _this.setState(1);
        _this.timecount = 60;
        _this.counter = $interval(_this.startCounnt, 1000);
      }
    })
  }

  $scope.timecount = 60;
  $scope.startCounnt = function() {
    _this.timecount--;
    if (_this.timecount <= 0) {
      $interval.cancel(_this.counter);
    }
  }

  $scope.checkCode = function() {
    // console.log(this.phonecode);
    if (this.phonecode) {
      UserService.checkCode({
        value: this.phonecode
      }).$promise.then(function(response) {
        if (UtilsService.isResponseOk(response)) {
          _this.setState(2);
        }
      })
    }
  }

  $scope.setPwd = function() {
    if (this.pwd) {
      UserService.setPwd({
        value: this.pwd
      }).$promise.then(function(response) {
        if (UtilsService.isResponseOk(response)) {
          $state.transitionTo('login');
        }
      })
    }
  }
});
